"""
解除权限
"""
from pymysqlutil import MysqlUtil


class DeleteRole:
    def __init__(self):
        self.mysqlutil = MysqlUtil("db_qxgl")

    def submenu(self):
        menu = """
        1.解除角色权限
        2.解除用户角色
        0.退出
        """
        print(menu)

    def del_menu(self):
        print("角色权限表展示：")
        sql = "select rm.id,r.rolename,m.menuname from tb_role_menu rm,tb_menu m,tb_role r where rm.menuid = m.id and rm.roleid=r.id"
        datas = self.mysqlutil.query_all(sql)
        for data in datas:
            print(data)
        rm_id = int(input("请输入要解除权限的角色权限id："))
        sql = f"select * from tb_role_menu where id = {rm_id}"
        data = self.mysqlutil.query_one(sql)
        print(data)
        if len(data) > 0:
            sql = f"delete from tb_role_menu where id = {rm_id}"
            result = self.mysqlutil.operator_data(sql)
            if result == 1:
                print("解除权限成功")
            if result == 0:
                print("解除权限失败")
        else:
            print("您输入的角色权限id不存在！")


    def del_role(self):
        print("用户角色表展示：")
        sql = "select ur.id,r.rolename,u.username from tb_user_role ur,tb_user u,tb_role r where ur.userid = u.id and ur.roleid=r.id"
        datas = self.mysqlutil.query_all(sql)
        for data in datas:
            print(data)
        rm_id = int(input("请输入要解除用户的角色id："))
        sql = f"select * from tb_user_role where id = {rm_id}"
        data = self.mysqlutil.query_one(sql)
        if len(data) > 0:
            sql = f"delete from tb_role_menu where id = {rm_id}"
            result = self.mysqlutil.operator_data(sql)
            if result == 1:
                print("解除角色成功")
            if result == 0:
                print("解除角色失败")
        else:
            print("您输入的用户角色id不存在！")

    def operation(self):
        while True:
            self.submenu()
            num = int(input("请输入选择操作的数字："))
            if num == 1:
                print("解除角色权限")
                self.del_menu()
            elif num == 2:
                print("解除用户角色")
                self.del_role()
            elif num == 0:
                print("退出成功")
                break
            else:
                print("输入错误，请重新选择操作：")
                continue



